
#include "cx_predef.h"
#include "cx_string.h"

#define STRING1 "abcjo9073-192.168"
#define STRING1_W L"abcjo9073-192.168"

#ifdef WIN32
#define cx_trace printf
#endif

#define  TEST_FUNCTION(_X) \
if (_X){  \
	goto FAILED; \
} \



QINT test_strlen()
{
	int len = 0;
	if ((len = cx_strlen(STRING1)) == cx_strlen_w(STRING1_W))
	{
		cx_trace("strlen is ok ![%d] \n",len);
		return 0;
	}
	else
	{
		cx_trace("strlen is failed !\n");
		return -1;	
	}
}

QINT test_strcmp()
{
	if(!(cx_strcmp_w(L"abcjo9073-192.168",STRING1_W) == cx_strcmp("abcjo9073-192.168",STRING1)))
	{
		goto FAILED;
	}
	if(!(cx_strcmp_w(L"aaa",STRING1_W) == cx_strcmp("aaa",STRING1)))
	{
		goto FAILED;
	}
	if (cx_strcmp_w(L"bcd",STRING1_W) <= 0)
	{
		goto FAILED;
	}
	cx_trace("strcmp is ok!\n");
	return 0;

FAILED:
	cx_trace("strcmp is failed!\n");
	return -1;
}

QINT test_strncmp()
{
	if(!(cx_strncmp_w(L"abcjo9073-192.168",STRING1_W,17) == cx_strcmp("abcjo9073-192.168",STRING1,17)))
	{
		goto FAILED;
	}
	if(!(cx_strncmp_w(L"abcjo9073-192.168",STRING1_W,10) == cx_strcmp("abcjo9073-192.168",STRING1,10)))
	{
		goto FAILED;
	}
	if(!(cx_strncmp_w(L"aaa",STRING1_W,3) == cx_strncmp("aaa",STRING1,3)))
	{
		goto FAILED;
	}
	if (cx_strncmp_w(L"bcd",STRING1_W,3) <= 0)
	{
		goto FAILED;
	}
	cx_trace("strncmp is ok!\n");
	return 0;
	
FAILED:
	cx_trace("strncmp is failed!\n");
	return -1;
}

QINT test_strcpy()
{
	QCHAR string[50];

	memset(string,0,50);
	cx_strcpy(string,STRING1);
	if (cx_strcmp(string,STRING1))
	{
		goto FAILED;
	}

	cx_strcpy_w(string,STRING1_W);
	if (cx_strcmp_w(string,STRING1_W))
	{
		goto FAILED;
	}
	cx_trace("strcpy is ok!\n");
	return 0;
FAILED:
	cx_trace("strcpy is failed!\n");
	return -1;
}

QINT test_strncpy()
{
	QCHAR string[50];

	memset(string,0,50);
	cx_strncpy(string,STRING1,10);
	if (cx_strncmp(string,STRING1,10))
	{
		goto FAILED;
	}
	memset(string,0,50);
	cx_strncpy_w(string,STRING1_W,10);
	if (cx_strncmp_w(string,STRING1_W,10))
	{
		goto FAILED;
	}

	cx_trace("strncpy is ok!\n");
	return 0;

FAILED:
	cx_trace("strncpy is failed !\n");
	return -1;
}

QINT test_strcat()
{
	QCHAR string[50];
	
	cx_strncpy(string,STRING1,10);
	string[10] = '\0';
	strcat(string,"192.168");
	if (cx_strcmp(string,STRING1))
	{
		goto FAILED;
	}

	cx_strcpy_w(string,L"abcjo9073-");
	cx_strcat_w(string,L"192.168");
	if (cx_strcmp_w(string,STRING1_W))
	{
		goto FAILED;
	}
	
	cx_trace("strcat is ok!\n");
	return 0;
	
FAILED:
	cx_trace("strcat is failed\n");
	return -1;
}

QINT test_stricmp()
{
	if (!(cx_stricmp("ABCD","abcd") == cx_stricmp_w(L"ABCD",L"abcd")))
	{
		goto FAILED;
	}
	if (cx_stricmp_w(L"ABCD",L"AAA") <= 0)
	{
		goto FAILED;
	}
	if (cx_stricmp_w(L"ABCD",L"Acd") >= 0)
	{
		goto FAILED;
	}

	cx_trace("stricmp is ok!\n");
	return 0;
FAILED:
	cx_trace("stricmp is failed!\n");
	return -1;
}

QINT test_strnicmp()
{
	if (!(cx_strnicmp("abcjo9073-",STRING1,10) == cx_strnicmp_w(L"abcjo9073-",STRING1_W,10)))
	{
		goto FAILED;
	}
	if (cx_strnicmp_w(STRING1_W,L"abcjo9073-292",13) >= 0)
	{
		goto FAILED;
	}
	if (cx_strnicmp_w(L"abcjo9073-292",STRING1_W,13) <= 0)
	{
		goto FAILED;
	}

	cx_trace("strnicmp is ok!\n");
	return 0;

FAILED:
	cx_trace("strnicmp is failed!\n");
	return -1;
}

QINT test_strchr()
{
	QCHAR string[50];
	QTCHAR *p;

	p = cx_strchr(STRING1,'9');
	cx_strcpy(string,p);
	if (cx_strcmp(string,"9073-192.168"))
	{
		goto FAILED;
	}
	
	p = cx_strchr_w(STRING1_W,'9');
	cx_strcpy_w(string,p);
	if (cx_strcmp_w(string,L"9073-192.168"))
	{
		goto FAILED;
	}
	if (NULL != cx_strchr_w(STRING1_W,'E'))
	{
		goto FAILED;
	}

	cx_trace("strchr is ok!\n");
	return 0;
FAILED:
	cx_trace("strchr is failed!\n");
	return -1;
}

QINT test_strcspn()
{
	if(cx_strcspn(STRING1,"abcjo9073"))
	{
		goto FAILED;
	}

	if (cx_strcspn_w(STRING1_W,L"abcjo9073"))
	{
		goto FAILED;
	}
	if (!(9 == cx_strcspn_w(STRING1_W,L"-1")))
	{
		goto FAILED;
	}
	cx_trace("strcspn is ok!\n");
	return 0;
FAILED:
	cx_trace("strcspn is failed!\n");
	return -1;
}

QINT test_strspn()
{

	if (!(cx_strspn(STRING1,STRING1) == cx_strspn_w(STRING1_W,STRING1_W)))
	{
		goto FAILED;
	}
	if (!(10 == cx_strspn_w(STRING1_W,L"abcjo9073-")))
	{
		goto FAILED;
	}

	cx_trace("strspn is ok!\n");
	return 0;
FAILED:
	cx_trace("strspn is failed!\n");
	return -1;
}

QINT test_strstr()
{
	QCHAR *p;

	p = cx_strstr(STRING1,"192");
	if (cx_strncmp(p,"192",3))
	{
		goto FAILED;
	}

	p = cx_strstr_w(STRING1_W,L"192");
	if (cx_strncmp_w(p,L"192",3))
	{
		goto FAILED;
	}
	if (cx_strstr_w(STRING1_W,L"de"))
	{
		goto FAILED;
	}

	cx_trace("strstr is ok!\n");
	return 0;
FAILED:
	cx_trace("strstr is failed!\n");
	return -1;
}


int test_string()
{
	cx_trace("********Test Begin !********\n");

	TEST_FUNCTION(test_strlen())
	TEST_FUNCTION(test_strcmp())
	TEST_FUNCTION(test_strncmp())
	TEST_FUNCTION(test_strcpy())
	TEST_FUNCTION(test_strncpy())
	TEST_FUNCTION(test_strcat())
	TEST_FUNCTION(test_stricmp())
	TEST_FUNCTION(test_strnicmp())
	TEST_FUNCTION(test_strchr())
	TEST_FUNCTION(test_strcspn())
	TEST_FUNCTION(test_strspn())
	TEST_FUNCTION(test_strstr())

	cx_trace("Everything is OK!\n");
	cx_trace("********Test End !********\n");
	return 0;

FAILED:
	cx_trace("Please check wrong!\n");
	return -1;
}